﻿using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Data.Repository.Enum;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class AddListCloseDaoRequestServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateCloseDaoRequestRequest, ResponseRecord>
    {
        private UserRecord currentUser;
        public AddListCloseDaoRequestServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(UpdateCloseDaoRequestRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.ListCloseDaoRequest == null || request.ListCloseDaoRequest.Count == 0)
            {
                throw new ArgumentException("Record must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var record = Request.ListCloseDaoRequest;
            try
            {
                List<string> listDao = record.Select(x => x.Dao).ToList();
                var listCif = Providers.Customer.RetrieveListCustomerByDao(listDao);
                List<ChangeDaoProduct> listProduct = Providers.ChangeDao.RetrieveListChangeDaoProductByDao(listDao);
                List<string> listDaoChecked = new List<string>();
                if (listCif.Count > 0)
                {
                    listDaoChecked.AddRange(listCif.Select(x => x.Dao).Distinct().ToList());
                }
                if (listProduct.Count > 0)
                {
                    listDaoChecked.AddRange(listProduct.Select(x => x.Dao).Distinct().ToList());
                }
                var userQuestion = record.Where(x => !listDaoChecked.Contains(x.Dao)).Select(x => new CloseDaoRequest
                {
                    Pic = currentUser.UserName,
                    BranchCodeSme = currentUser.BranchCode,
                    CreatedDate = DateTime.Now,
                    UpdateDate = DateTime.Now,
                    Dao = x.Dao,
                    LinkOfferLetter = string.Empty,
                    Status = (int)StatusCloseDaoEnum.Request
                }).ToList();
                var listDaoFail = record.Where(x => listDaoChecked.Contains(x.Dao)).Select(x => x.Dao).ToArray();
                if (listDaoFail.Count() > 0)
                {
                    response.ErrorMessage = string.Format("DAO: {0} vẫn còn gắn với khách hàng, chưa thể đóng được", string.Join(",", listDaoFail));
                    response.IsSuccess = false;
                }
                else
                {
                    var listDaoExist = new List<string>();
                    bool savedUser = Providers.CloseDao.SaveCloseDaoRequest(out listDaoExist, userQuestion);
                    if (listDaoExist.Count > 0)
                    {
                        response.ErrorMessage = string.Format("DAO: {0} đã được đóng", string.Join(",", listDaoExist));
                        response.IsSuccess = false;
                    }
                    else
                    {
                        response.IsSuccess = savedUser;
                    }
                }
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
                response.IsSuccess = false;
            }
            return response.IsSuccess;
        }
    }
}
